//=======================================================================
//功能: 			JavaScript 分页工具类
//						自动生成表格等
//author:		岳静
//e-mail: 		yuejing0129@126.com
//QQ:			503490146
//date: 		2012-08-01
//version: 	1.0
//=======================================================================

/**
 * 获取Page对象
 * _page		创建的Page对象的名称
 * _infoId		显示内容的控件的ID
 * _callback	查询的函数
 * _infoType	显示内容的控件类型[不传入默认为table类型]
 */
var Page = function(_page, _infoId, _callback, _infoType) {
	this.pageName = _page;
	this.infoObj = document.getElementById(_infoId);
	if(_infoType == undefined || _infoType == "")
		_infoType = this.infoTypeArray[0];
	this.callback = _callback;
	this.infoType = _infoType;
};

//添加展示信息类型的属性
Page.prototype.infoTypeArray = [ "table" ];
//添加展示信息类型的内容信息
Page.prototype.infoTypeContextArray = ["<table width='100%' border='0' cellpadding='0' cellspacing='0' class='table_page'>" ];

//当前页[默认为第一页]
Page.prototype.page = 1;
//总页数大小
Page.prototype.countPage;
//每页大小[默认为10条]
Page.prototype.size = 10;

//page间距大小[默认为3]
Page.prototype.pageDiffNum = 3;

//要展示的标题的集合
Page.prototype.titleArray;
//要展示的属性的集合
Page.prototype.fieldArray;
//操作的信息集合
Page.prototype.operateJson;

//批量操作
Page.prototype.batchOperate;

//table合并的行数
Page.prototype.tableColspanNum = function() {
	var colspanNum = this.fieldArray.length;
	if(this.operateJson != undefined && this.operateJson.length > 0) {
		colspanNum ++;
	}
	if(this.batchOperate != undefined && this.batchOperate.detail != undefined && this.batchOperate.detail.length > 0) {
		colspanNum ++;
	}
	return colspanNum;
};

//添加请求中方法
Page.prototype.pageBeforeSend = function(message) {
	if(message == undefined || message == '') {
		message = "加载中...";
	}
	//为table
	if(this.infoType == this.infoTypeArray[0]) {

		var result = this.infoTypeContextArray[0] + "<tr><td colspan='" + this.tableColspanNum() + "'>" + message + "</td></tr></table>";
		this.infoObj.innerHTML = result;
	}
};

//添加失败处理结果方法
Page.prototype.pageError = function(message) {
	if(message == undefined || message == '') {
		message = "加载信息异常, 请稍候再试...";
	}
	//为table
	if(this.infoType == this.infoTypeArray[0]) {
		var colspanNum = this.fieldArray.length;
		if(this.operateJson != undefined && this.operateJson.length > 0) {
			colspanNum ++;
		}
		var result = this.infoTypeContextArray[0] + "<tr><td colspan='" + colspanNum + "'>" + message + "</td></tr></table>";
		this.infoObj.innerHTML = result;
	}
};

//添加成功处理结果方法
Page.prototype.pageSuccess = function(json, fields) {
	//根据table处理结果
	if(this.infoType == this.infoTypeArray[0]) {
		if(json != undefined && json) {
			if(this.fieldArray == undefined || this.fieldArray.length <= 0) {
				throw new Error("请设置要显示的属性集合!");
			}
			this.countPage = json.countPAGE;
		}
		this.tableResult(json);
	}
};

//对批量操作的全选或全不选
Page.prototype.selectChk = function(obj) {
	var chks = document.getElementsByName(this.batchOperate.widgetId);
	for(var i = 0; i < chks.length; i ++) {
		chks[i].checked = obj.checked;
	}
};

//根据table类型处理结果
Page.prototype.tableResult = function(json) {
	var result = this.infoTypeContextArray[0];
	//处理标题
	if(this.titleArray != undefined && this.titleArray.length > 0) {
		result += "<tr>";
		//批量操作全选/全不选
		if(this.batchOperate != undefined && this.batchOperate.detail != undefined && this.batchOperate.detail.length > 0) {
			result += "<th><input type='checkbox' onchange='" + this.pageName + ".selectChk(this)'/></th>";
		}
		for (var t in this.titleArray) {
			result += "<th>" + this.titleArray[t] + "</th>";
		}
		result += "</tr>";
	}
	if(json != undefined && json) {
		var jsonData = json.DATA;
		if(jsonData != undefined && jsonData.length > 0) {
			for (var i in jsonData) {
				//处理集合中的单个对象
				result += "<tr>";
				//批量操作
				if(this.batchOperate != undefined && this.batchOperate.detail != undefined && this.batchOperate.detail.length > 0) {
					result += "<td align='center'><input type='checkbox' id='" + this.batchOperate.widgetId + i + "' class='" + this.batchOperate.widgetId + "' name='" + this.batchOperate.widgetId + "'/></td>";
				}
				//获取显示的属性的值
				for (var f in this.fieldArray) {
					result += "<td>" + jsonData[i][this.fieldArray[f]] + "</td>";
				}
				//操作
				if(this.operateJson != undefined && this.operateJson.length > 0) {
					result += "<td align='center'>";
					for(var o in this.operateJson) {
						//根据条件处理
						if(this.operateJson[o].termField != undefined && this.operateJson[o].termField.length > 0) {
							var isShow = false;
							for(var ter in this.operateJson[o].termField) {
								if(jsonData[i][this.operateJson[o].termField[ter]] == this.operateJson[o].termValue[ter]) {
									isShow = true;
									break;
								}
							}
							if(!isShow) {
								continue;
							}
						}
						var url = this.operateJson[o].url;
						//处理url后面的参数
						if(this.operateJson[o].fieldName != undefined) {
							//判断url中是否有javascript:
							if(url.indexOf("javascript:") > -1) {
								url += "(";
								for(var pf in this.operateJson[o].fieldName) {
									url += "\"" + jsonData[i][ this.operateJson[o].fieldValue[pf] ] + "\",";
								}
								var index = url.lastIndexOf(",");
								if(index > -1) {
									url = url.substring(0, index) + ")";
								}
							} else {
								if(this.operateJson[o].fieldName.length > 0) {
									if(url.lastIndexOf("?") < 0) {
										url += "?";
									} else {
										url += "&";
									}
									for(var pf in this.operateJson[o].fieldName) {
										url += this.operateJson[o].fieldName[pf];
										url += "=" + jsonData[i][ this.operateJson[o].fieldValue[pf] ] + "&";
									}
									url = url.substring(0, url.length - 1);
								}
							}
						}
						result += "<a class='table_page_operate_a' href='" + url + "'>" + this.operateJson[o].name + "</a>&nbsp;";
					}
					result += "</td>";
				}
				result += "</tr>";
			}

			//生成分页的信息
			result += this.pageOperate();
		} else {
			result += "<tr><td colspan='" + this.tableColspanNum() + "'>没有信息!</td></tr>";
		}
	} else {
		result += "<tr><td colspan='" + this.tableColspanNum() + "'>没有信息!</td></tr>";
	}
	result += "</table>";
	this.infoObj.innerHTML = result;
};

//分页的信息[上一页 下一页 尾页 等信息...]
Page.prototype.pageOperate = function(type, json) {
	//批量操作
	var batchString = "";
	if(this.batchOperate != undefined && this.batchOperate.detail != undefined && this.batchOperate.detail.length > 0) {
		for(var batch in this.batchOperate.detail) {
			batchString += "<a class='table_page_batch_operate_a' href='javascript: "+this.batchOperate.detail[batch].callback+"'>" + this.batchOperate.detail[batch].name + "</a>&nbsp;&nbsp;";
		}
	}
	var result = "";
	if(type != "custom") {
		result = "<tr class='table_page_foot'><td colspan='" + this.tableColspanNum() + "'>";
	} else if(type == "custom") {
		//设置信息
		this.countPage = json.countPAGE;
	}
	result += "&nbsp;&nbsp;" + batchString;
	//result += "每页大小&nbsp;" + this.size + "条&nbsp;&nbsp;";
	result += "第&nbsp;" + this.page + "页&nbsp;&nbsp;";
	result += "共&nbsp;" + this.countPage + "页&nbsp;&nbsp;";

	var _page = this.page;

	if(_page > 1)
		result += "<a class='table_page_num_a' href='javascript: " + this.pageName + ".pageFun(1)'>上一页</a>&nbsp;&nbsp;";

	var startNum;
	//当前页减去距离页大于1
	if( (_page - this.pageDiffNum) > 1 ) {
		result += "<a class='table_page_num_a' href='javascript: " + this.pageName + ".pageFun(0)'>1</a>&nbsp;&nbsp;..&nbsp;&nbsp;";
		startNum = (_page - this.pageDiffNum);
	} else {
		if( (_page + this.pageDiffNum) > this.countPage )
			_page = _page + this.pageDiffNum;
		startNum = 1;
	}
	//当前页加上距离页小于总页数
	var endResult;
	if( (_page + this.pageDiffNum) < this.countPage ) {
		endResult = "..&nbsp;&nbsp;<a class='table_page_num_a' href='javascript: " + this.pageName + ".pageFun(3)'>" + this.countPage + "</a>&nbsp;&nbsp;";
		if( (_page + this.pageDiffNum) < this.countPage )
			endNum = (_page + this.pageDiffNum);
		else
			endNum = this.countPage;
	} else {
		endNum = this.countPage;
		if( (_page + this.pageDiffNum) < this.countPage )
			startNum = endNum - (2 * this.pageDiffNum + 1);
	}
	//处理中间的数字
	for(var i = startNum; i <= endNum; i ++) {
		if(i != this.page) {
			result += "<a class='table_page_num_a' href='javascript: " + this.pageName + ".pageFun(4, " + i + ")'>" + i + "</a>&nbsp;&nbsp;";
		} else {
			result += "<font class='table_page_num_select'>" + i + "</font>&nbsp;&nbsp;";
		}
	}
	if(endResult != undefined)
		result += endResult;
	if(_page < this.countPage)
		result += "<a class='table_page_num_a' href='javascript: " + this.pageName + ".pageFun(2)'>下一页</a>&nbsp;&nbsp;";

	//处理size大小
	result += "&nbsp;&nbsp;页大小&nbsp;";
	for(var i = 10; i <= 30; (i = i + 10) ) {
		if(i != this.size) {
			result += "<a class='table_page_num_a' href='javascript: " + this.pageName + ".pageFun(5, " + i + ")'>" + i + "</a>&nbsp;&nbsp;";
		} else {
			result += "<font class='table_page_num_select'>" + i + "</font>&nbsp;&nbsp;";
		}
	}
	if(type != "custom") {
		result += "</td></tr>";
	}
	return result;
};

//分页的信息[上一页 下一页 尾页 等信息...]
Page.prototype.pageFun = function(_type, num) {
	switch (_type) {
	case 0:
		//首页
		this.page = 1;
		break;
	case 1:
		//上一页
		if(this.page > 1)
			this.page --;
		break;
	case 2:
		//下一页
		if(this.page < this.countPage)
			this.page ++;
		break;
	case 3:
		//末页
		this.page = this.countPage;
		break;
	case 4:
		//自定义页码
		this.page = num;
		break;
	case 5:
		//自定义每页的大小
		this.size = num;
		break;

	default:
		break;
	}
	this.callback();
};

//jQuery的Ajax查询
Page.prototype.jqueryAjax = function(page, _url, _data, _type, _dataType, _errorMessage, _beforeSendMessage, _async) {
	_data["page"] = page.page;
	_data["size"] = page.size;
	jQuery.ajax({
		url : _url,
		data : _data,
		type : _type,
		dataType : _dataType,
		async : (_async == undefined ? true : false),
		beforeSend: function(){
		//提交处理的函数
		page.pageBeforeSend(_beforeSendMessage);
	},
	error : function(json){
		page.pageError(_errorMessage);
	},
	//返回的是一个对象
	success : function(json){
		page.pageSuccess(json);
	}
	});
};